package com.lanjing.common.core.result;

import com.lanjing.common.core.enums.ResultEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private static final int SUCCESS_CODE = ResultEnum.SUCCESS.getCode();
    private static final int FAILURE_CODE = ResultEnum.ERROR.getCode();
    private static final String DEFAULT_SUCCESS_MSG = ResultEnum.SUCCESS.getMessage();

    // 响应码
    private int code = SUCCESS_CODE;
    // 消息
    private String message;
    // 数据
    private T data;

    public boolean isSuccess(){
        return this.code == SUCCESS_CODE;
    }

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(String message) {
        this.message = message;
    }

    public Result(ResultEnum responseEnum) {
        if (responseEnum != null) {
            this.code = responseEnum.getCode();
            this.message = responseEnum.getMessage();
        }
    }

    public static <T> Result<T> success() {
        return new Result<>(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data){
        return new Result<>(data);
    }

    public static <T> Result<T> fail(String message){
        return new Result<>(message);
    }

    public static <T> Result<T> fail(ResultEnum resultEnum){
        return new Result<>(resultEnum.getCode(),resultEnum.getMessage());
    }
}
